<?php

namespace app\merchants\controller;

use app\common\validate\merchant\ProductClassValid;
use app\common\validate\merchant\ProductValid;
use app\merchants\loader\Services;
use think\Exception;

/**
 * 商品管理|分类管理
 */
class Product extends ApiBase
{

    /**
     * 商品分页列表
     * @return  void
     */
    public function product_page(): void
    {
        try {
            $data = input('get.');
            //需要获取商户id
            $where[] = ['pro.mer_id', '=', $this->adminUser['mer_id']];
            //删除时间
            $where[] = ['pro.delete_at', '=', null];

            if (!empty($data['keyword'])) {
                $where[] = ['pro.title|pro.code', 'like', '%' . $data['keyword'] . '%'];
            }

            if (!empty($data['class_id'])) {
                $where[] = ['pro.class_id', '=', $data['class_id']];
            }

            $res = Services::Product()->Propage($where, $data['page'] ?? 1, $data['page_size'] ?? 30);
            if (!empty($res['data'])) {
                $res['data'] = Services::Virtual()->showVirtualPromo($res['data'], $this->adminUser['mer_id'], $data['vp_id'] ?? 0);
            }
            api_result($res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商品保存
     * @return  void
     * @throws \Exception
     */
    public function product_save(): void
    {
        $param = input('post.');

        try {
            //数据拦截验证
            validate(ProductValid::class)->scene('product_save')->check($param);
            $array = array_merge($param, ['op_user' => $this->adminUser['admin_id'], 'mer_id' => $this->adminUser['mer_id']]);

            $id = (int)($param['id'] ?? 0);
            Services::Product()->save($array, $id);
            api_result(API_SUCCESS, '保存成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商品删除
     * @return  void
     * @throws \Exception
     */
    public function product_del(): void
    {
        $data = input('post.');
        try {
            validate(ProductValid::class)->scene('product_del')->check($data);

            Services::Product()->del($data['id'], $this->adminUser['mer_id']);

            api_result(API_SUCCESS, '请求成功', $data['id']);
        } catch (Exception $e) {
            api_result($e);
        }


    }

    /**
     * 商品详情
     * @return  void
     * @throws \Exception
     */
    public function product_info(): void
    {
        $data = input('get.');
        try {
            validate(ProductValid::class)->scene('product_info')->check($data);
            $data = Services::Product()->info($data['id']);
            api_result(API_SUCCESS, '请求成功', $data);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商品修改库存
     * @return  void
     * @throws \Exception
     */
    public function product_save_stock(): void
    {
        $data = input('post.');
        try {
            validate(ProductValid::class)->scene('product_save_stock')->check($data);
            $data['mer_id'] = $this->adminUser['mer_id'];
            Services::Product()->save_stock($data, $data['id']);

            api_result(API_SUCCESS, '请求成功', $data['id']);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商品列表
     */
    public function product_list(): void
    {
        try {
            $data = input('get.');
            if (!empty($data['keyword'])) {
                $where[] = ['title|code', 'like', '%' . $data['keyword'] . '%'];
            }
            $where[] = ['mer_id', '=', $this->adminUser['mer_id']];
            $where[] = ['delete_at', '=', null];
            $limit = (int)(empty($data['limit']) ? 10 : $data['limit']);
            $field = ['id', 'code', "concat(code,'|',title) as title"];
            $result = Services::Product()->list($where ?? [], $limit, $field);
            api_result(API_SUCCESS, '请求成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

//商户商品分类============================================================================

    /**
     * 分类分页列表
     */
    public function class_list(): void
    {
        try {
            //需要获取商户id
            $data = input('get.');
            $where[] = ['mer_id', '=', $this->adminUser['mer_id']];
            if (!empty($data['keyword'])) {
                $where[] = ['title', '=', $data['keyword']];
            }
            // 父ID
            $pid = (int)(empty($data['pid']) ? 0 : $data['pid']);
            $where[] = ['pid', '=', $pid];

            //
            $limit = (int)(empty($data['limit']) ? 10 : $data['limit']);
            $data = Services::ProductClass()->getList(['where' => $where], 0);
            api_result($data);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 分类保存
     * @throws \Exception
     */
    public function class_save(): void
    {
        try {
            $data = input('post.');
            validate(ProductClassValid::class)->scene('class_save')->check($data);
            $array = array_merge($data, ['op_user' => $this->adminUser['admin_id'], 'mer_id' => $this->adminUser['mer_id']]);

            $id = (int)(empty($data['id']) ? 0 : $data['id']);
            $id = Services::ProductClass()->save($array, $id);
            api_result(API_SUCCESS, '请求成功', ['id' => $id]);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 分类删除
     */
    public function class_del(): void
    {
        try {
            $data = input('post.');
            validate(ProductClassValid::class)->scene('class_del')->check($data);
            Services::ProductClass()->del($data['id'], $this->adminUser['mer_id']);
            api_result(API_SUCCESS, '请求成功', $data['id']);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 分类详情
     * @return  void
     */
    public function class_info(): void
    {
        try {
            $data = input('get.');
            validate(ProductClassValid::class)->scene('class_info')->check($data);
            $data = Services::ProductClass()->info($data['id']);
            api_result(API_SUCCESS, '请求成功', $data);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 树形商品分类
     * @return void
     */
    public function class_tree_list()
    {
        try {
            $option = [];
            $where[] = ['mer_id', '=', $this->adminUser['mer_id']];
            if (!empty($this->adminUser['sub_admin_ids'])) {
                $admin_id = $this->adminUser['sub_admin_ids'];
                $where[] = ['id', 'in', function ($query) use ($admin_id) {
                    $query->name('merchant')->whereIn('id', $admin_id)->field('dept_id');
                }];
            }
            $option['where'] = $where;
            $result = Services::ProductClass()->getList($option);
            api_result($result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 分类修改排序
     * @return  void
     */
    public function class_save_sort(): void
    {
        try {
            $data = input('post.');
            validate(ProductClassValid::class)->scene('class_save_sort')->check($data);
            Services::ProductClass()->save_sort($data['id'], $data['sort']);
            api_result(API_SUCCESS, '请求成功', $data['id']);
        } catch (Exception $e) {
            api_result($e);
        }
    }
}

